$(function(){

    $(document).ready(function(){
	var loading = '<dl class="slInfo" style="border: none;margin:10px 0 0 0;height:40px;"><div class="loading">加载中...</div></dl>'; 
    $("#page").append(loading);
	$.get(jumpUrl+'/center/mylawyer/followed', function(data){
  		}, "json").error(function(data) {
    
  		}).success(function(data) {
  			$(".loading").parent().remove();
  			var obj = data.data;
          	// console.info(obj)
			var str  = "";
			for(var i=0; i<obj.length; i++)  
            	{
				str +='<dl class="slInfo clear">'
                  str +='<dt>'
                      str +='<a href="'+jumpUrl+'/lawyer/'+obj[i].id+'"><img class="lazy" data-original="'+obj[i].avatar+'" src="'+obj[i].avatar+'" width="100" height="100" alt="个人头像" /></a>'
                  str +='</dt>'
                  str +='<dd class="slContxt" style="width:680px;">'
                      str +='<div class="slContxt-details clearfix" style="width:450px;">'
                        str +='<div class="details-one clearfix" style="width:200px;">'
                          str +='<a class="name" href="'+jumpUrl+'/lawyer/'+obj[i].id+'">'
                              str +='<i>'+obj[i].realname+'</i>'
                              str +='<em>律师</em>'
                            str +='</a>'
                            str +='<span>'+obj[i].province+'/'+obj[i].city+'</span>'
                        str +='</div>'
                        str +='<div class="details-two" style="width:200px;">'
                          str +='<div class="clearfix">'
                            str +='<span>好评：</span>'
                            for (var startJudgment = 0; startJudgment < 5; startJudgment++) {
                              if (startJudgment < obj[i].average_evaluate_score.length) {
                                str +='<img src="'+jumpUrl+'/img/star-on.png" alt="" />'
                              } else {
                                str +='<img src="'+jumpUrl+'/img/star-off.png" alt="" />'
                              }
                            }
                          str +='</div>'
                          str +='<div class="clearfix">'
                            str +='<span>被关注：'+obj[i].follows_count+'&nbsp&nbsp次</span>'
                          str +='</div>'
                          str +='<div class="clearfix">'
                            str +='<span>执业年限：'+obj[i].practice_period+'</span>'
                          str +='</div>'
                        str +='</div>'
                        str +='<div class="details-three clearfix">'
                          // console.log(obj[i].work_scope.length)
                          // str +='<span>'+obj[i].work_scope[0].value+'</span>'
                          if (obj[i].work_scope.length >= 3) {
                            obj[i].work_scope.length = 3;
                          }
                          for (var beGood = 0; beGood < obj[i].work_scope.length; beGood++) {
                            str +='<span>'+obj[i].work_scope[beGood].value+'</span>'
                          }
                        str +='</div>'
                      str +='</div>'
                      str +='<div class="slContxt-complete">'
                        str +='<div>完成：'+obj[i].pay_reply_count+'&nbsp&nbsp单</div>'
                        // str +='<a href="javascript:void(0)" class="question_but" data-id="'+obj[i].phone_reply_fee+'">向他咨询</a>'
                        str +='<a class="type_reply_fee" style="display:none" data-id="'+obj[i].text_reply_fee+'">已关注</a>'
                          str +='<a class="attention " data-id="'+obj[i].id+'" href="javascript:void(0)"><i class="zanicon"></i>取消关注</a>'
                      str +='</div>'
                  str +='</dd>'
                  
              str +='</dl>'
		        }  

			$("#page").append(str);
			$("#page dl").css("margin-bottom","-1px")

		});
	}); 
  //取消关注
	  $(document).on('click', '.attention', function(e) {
	      var follow_id = $(this).attr("data-id");
	      var token = $("input:[name='_token']").val();
	      var params = {};
        var $this = $(this);
	      params['follow_id'] = follow_id;
	      params['user_id'] = user_id;
	      params['_token'] = token;
	      console.info(params)
	      var url = jumpUrl+'/follow/remove';
	      $.post(url,params,function(data){

	        }, "json").error(function(data) {

	          var error =  $.parseJSON(data.responseText);
	          $.each(error.error_messages, function(i, item){
	            layer.msg(item[0]);
	          });
	        }).success(function(data) {
	          var obj = data.data;
	          layer.msg(obj);
            $this.parent().parent().parent().css("display","none")
            // window.location.href=jumpUrl+"/center/mylawyer/followed";
	        })
	    stopDefault(e);
	  });



    //获取擅长领域的数据
    $.get(user_lawyer_url, function(data){
        }, "json").error(function(data) {
          var status_code = $.parseJSON(data.responseText);
          layer.msg(status_code.message);
        }).success(function(data) {
           var obj = data.data;
            var work_scope = obj;
            var options = '';
            $.each(work_scope, function(key, item) {
                options+='<option value="'+item.key+'">'+item.value+'</option>';
            });
            $("#work_scope").append(options);
        });
 // 付费咨询 弹出框
   $(document).on('click', '.question_but', function(e) {
        var Popup02 = $("#consulting");
        $("#consulting").css("display","block")
        var lawyer_id = $(this).siblings(".attention").attr("data-id");
        var question_text = $(this).siblings(".type_reply_fee").attr("data-id");
        var question_phone = $(this).attr("data-id");
        $(".text_reply_fee").text(question_text)
        $(".phone_reply_fee").text(question_phone)
        $("#lawyer_id").val(lawyer_id)
        layer.open({
          type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
          title: false, //标题
          closeBtn: true,  //关闭按钮
          shade:0.3,  //遮罩默认是0.3透明度
          area: ['477px', '515px'],
          skin: 'yourclass', //样式类名
          content: Popup02 //内容
      }); 
    }); 
//提交咨询
      $(".quesissSbmint .subAdvisory").click( function (e) {
          var title = $(".quesIssContext #title").val();
          var content = $(".quesIssContext #content").val();
          var work_scope = $(".quesIssContext #work_scope").val();
          if(work_scope.length < 1){
              layer.msg('请选择案件类型！', {time: 2000});
              return false;
          }else if(title.length<1){
              layer.msg('标题不能为空！', {time: 2000});
              return false;
          }else if(content.length < 1){
              layer.msg('内容不能为空！', {time: 2000});
              return false;
          }else if(content.length < 20){
              layer.msg('内容字数不能小于20字！', {time: 2000});
              return false;
          }else{var loading = layer.msg('加载中...', {icon: 16});
          var inquery = $("#inquery_create_form");
          $(this).attr("disabled","disabled");
          $.post(inquery[0].action, inquery.serialize(),
             function(data){
                 // console.info(data);
                $(':input',inquery).not(':button, :submit, :reset, :hidden').val('');
                layer.msg("提交成功！感谢您的咨询，律团网的律师会尽快为您解答！");
                layer.close(loading);
             }, "json").error(function(data) {
               var obj = $.parseJSON(data.responseText);
               // console.info(obj);
               layer.msg(obj);
             }).success(function(data){
              var obj = data.data;
              var id = obj.id;
              window.location.href=jumpUrl+'/center/pay/question/'+id+'/to_pay';
             });
             
             return true;
      }
      stopDefault(e);
  });

  //上传图片的插件
      //初始化jQuery的文件上传控件
      var token = $("input:[name='_token']").val();
      $('#avatar_file').fileupload({
          limitMultiFileUploads : 1,
          limitMultiFileUploadSize : 1,
          limitMultiFileUploadSizeOverhead : 512,
          limitConcurrentUploads : 1,
            sequentialUploads : true,
          headers: 
            {"Accept":"application/json"}, //必须有
              dataType: "json", //表示返回值类型，不必须
          formData:{
            _token:token
          },
          messages: {uploadedBytes: 'Uploaded bytes exceed file size'},
          done: function (e, data) {//设置文件上传完毕事件的回调函数 
              var obj = data.result;
              var avatar = obj.data.file_path;
              var file_name = obj.data.file_name;
              var fileExt=( /[^.]+$/.exec(avatar.toUpperCase())); 
              var type = fileExt[0];
              $(".btn-success").append('<input type="hidden" name="file_paths[]" value="'+avatar+'"><input type="hidden" name="file_names[]" value="'+file_name+'">');
              layer.msg("上传文件成功！");
                $(".filename ul").append('<li>'+file_name+'&nbsp;&nbsp;<a class="delet" href="javascript:void(0);" style="color:#004799;" title="删除">删除</a></li>');//文件名
                $(".avatar_js_cover").show();
                 var fileLen = $(".filename ul li").length;
                 var nameLen = $("input:hidden[name='file_names[]']").length;
                 if(fileLen>=2){
                     $('#avatar_file').attr("disabled",true);
                 }
              
              
            },
            fail: function(e, data) {
                //错误提示
                /*console.info(data)*/
            }
        });

      //删除上传的文件
      $(document).on( "click", ".delet", function(e) {
       $this = $(this).parent();
       $this.hide();
       index = $this.index();
       $(this).parent().remove();
       $("input:hidden[name='file_paths[]']").eq(index).remove();
       $("input:hidden[name='file_names[]']").eq(index).remove();
       var fileLen = $(".filename ul li").length;
       var nameLen = $("input:hidden[name='file_names[]']").length;
       if(fileLen<2){
         $('#avatar_file').attr("disabled",false);
       }
        stopDefault(e);
      });

      // 监控文本内容长度
      var text_max_length = 300
      $("#info .text_length").text(text_max_length)
      $("#info .text_change").text(text_max_length)
      $('#content').bind('input propertychange', function() {
          
          var content = $("#content").val();
          if (content.length > 0) {
            $("#info .text_change").text(text_max_length-content.length).css("color","#004799")
          }else{
          $("#info .text_change").text(text_max_length-content.length).css("color","#999")
          }
      });

      // 演示html
      //<textarea  placeholder="请输入内容" id="content" name="content"></textarea>
      //<label id="info">还可以输入<span class="text_bian"></span>/<span class="text_length"></span>个字</label>



});